package me.leiusual.sharing.ui.home.adapter

import android.view.View
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_home_link.*
import me.leiusual.sharing.api.model.storeindex.ProductLink
import me.leiusual.sharing.ui.home.HomeHomeFragmentDirections
import me.leiusual.sharing.util.extension.setImageUrl

/**
 * Create by leiusual on 2018/7/24
 */
class HomeLinkViewHolder(
    override val containerView: View
) : RecyclerView.ViewHolder(containerView), LayoutContainer, View.OnClickListener {

  private lateinit var productLink: ProductLink

  init {
    containerView.setOnClickListener(this)
  }

  fun bind(productLink: ProductLink) {
    this.productLink = productLink

    text_view_title.text = productLink.title
    image_view_photo.setImageUrl(productLink.image)
  }

  override fun onClick(v: View?) {
    val modelInAreaId = productLink.modelInAreaId
    if (modelInAreaId != null) {
      val action = HomeHomeFragmentDirections.actionHomeHomeFragmentToProductDetailFragment(modelInAreaId, 0)
      containerView.findNavController().navigate(action)
    }
  }
}